package com.atguigu.tingshu.order.template;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * title:
 * author: zgl
 * date: 2024/3/16
 * description：
 */
public abstract class OrderConfirm {
    private RedisTemplate redisTemplate;

    public OrderConfirm() {
    }

    public OrderConfirm(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public OrderInfoVo trade(TradeVo tradeVo) {
        //创建订单确认页面对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        //生成交易号
        String tradeNo = IdWorker.getIdStr();
        orderInfoVo.setTradeNo(tradeNo);
        //根据tradeNo存入Redis 提交订单时进行幂等性校验
        this.redisTemplate.opsForValue().set(RedisConstant.TRADE_MDX_PREFIX + tradeNo, tradeNo, 24, TimeUnit.HOURS);
        //设置支付方式
        orderInfoVo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        //设置付款项目类型
        orderInfoVo.setItemType(tradeVo.getItemType());
        //调用confirm方法
        confirm(orderInfoVo, tradeVo);
        //设置时间戳
        orderInfoVo.setTimestamp(SignHelper.getTimestamp());
        //设置签名
        orderInfoVo.setSign(SignHelper.getSign(JSON.parseObject(JSON.toJSONString(orderInfoVo), HashMap.class)));
        //返回结果
        return orderInfoVo;
    }


    public abstract void confirm(OrderInfoVo orderInfoVo, TradeVo tradeVo);
}
